<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>world!!!</h1>
<button onclick="closeES()">关闭连接</button>
<div id="message"></div>
</body>
<script>
    var es = new EventSource("/sse");
    es.onopen = function (e) {
        console.log("open")
    };
    es.onmessage = function (e) {
        console.log(e.data, e.lastEventId);
        setMessageInnerHTML(e.data);
    }
    es.onerror = function (e) {
        console.log("error")
        // es.close()
    }

    //将消息显示在网页上
    function setMessageInnerHTML(innerHTML) {
        document.getElementById('message').innerHTML += innerHTML + '<br/>';
    }

    //关闭连接
    function closeES() {
        // console("232323")
        console.log("close...")
        es.close();
    }
</script>
</html>